<?php

/**
 * Utils Event
 * @author ReGeDa
 */
class Utils_Handler extends DB_Event_Handler {

	/**
	 * Database selection
	 * @param <type> $name 
	 */
	public function on_directory($table) {

		$cache = &platcode()->plugins->import('Memcacher');
		/* @var $cache Memcacher */

		// cacher key
		$key = 'table-'.$table;

		$data = $cache->read_cache($key);

		if ($data === FALSE) {

			/////// Read from Database

			$AQ = &platcode()->plugins->import('ActiveQuery');
			/* @var $AQ ActiveQuery */

			$stmt = $this->db->prepare(
				$AQ->from($table)->order('name')->select()
			);
			/* @var $stmt PDOStatement */

			$AQ->query($stmt);
			$data = $stmt->fetchAll(PDO::FETCH_ASSOC);

			// save in cache
			$cache->write_cache($key, $data);
		}

		platcode()->raw[$key] = $data;

	}

}

// end of file